<?php 
$output .= 'var tinyMCEMediaList = new Array(';
$delimiter = "\n";
$directory = "../../upload/medias"; // Use your correct (relative!) path here
$abspath = preg_replace('~^/?(.*)/[^/]+$~', '/$1', $_SERVER['SCRIPT_NAME']); // Since TinyMCE3.x you need absolute image paths

$TinyMceMediaExts =array( // allowed extensions
   'avi'=>'1', 'mpg'=>'1', 'mov'=>'1', 'fla'=>'1', 'swf'=>'1', 'wmv'=>'1'
);

if(is_dir($directory))
{   $direc = opendir($directory);
    while($file = readdir($direc))
	{	//if(!preg_match('~^.~', $file))// no hidden files / directories here...
		if(preg_match('~^.~', $file))// no hidden files / directories here...
		{	if(is_file("{$directory}/{$file}")) // We got ourselves a file! Make an array entry:
			{	preg_match('/\.([^.]+)$/',$file,$match);
                if($TinyMceMediaExts[@$match[1]])
				{	$output .= $delimiter
                    		. '["'.utf8_encode($file).'", "'.utf8_encode("../../upload/medias/{$file}").'"],';
               	}//end if
            }//end if
        }//end if
    }//end while loop
    $output = substr($output, 0, -1); // remove last comma from array item list (breaks some browsers)
    $output .= $delimiter;
    closedir($direc);
}//end if
$output .= ');';

header('Content-type: text/javascript'); // browser will now recognize the file as a valid JS file
header('pragma: no-cache');// prevent browser from caching
header('expires: 0'); // i.e. contents have already expired
echo $output;
?>